this.Food=this.Food||{};
(function(fd){
	fd.cache={};
})(this.Food);
/**
 * action配置
 */
(function(fd){
	//var pre='open_id=a&openkey=5F154D7D2751AEDC8527269006F290F70297B7E54667536C&sig=9999b41ad0b688530bb1b21c5957391c&pf=tap';
	var pre={
		'open_id':'a',
		'openkey':'xx',
		'sig':'xx',
		'pf':'xx'
	};
	fd.action={
		//首页欢迎语,硬编码
		index:function(){
			var hours=new Date().getHours(),
			wel=[4,4,4,4,4,/*5:59*/0,0,0,0,/*9:59*/1,1,1,1,/*13:59*/2,2,2,/*16:59*/3,3,3,3,/*20:59*/4,4,4,4],
			current=wel[hours],
			timeTxt=['美好的一天从吃开始！？','锄禾日当午，没吃饭最苦。','饮茶会友，偷得浮生半日闲。','两个人晚餐，终结了孤单。','药药药！切克闹！烧烤夜啤来一套！'],
			rdTxt=['喜欢梅子的清新，还是芥末的狂野？','爱温软如玉，也爱辣翻舌根；爱香氛脆爽，也爱酸里透甜。','唯有美食与爱不可辜负。','吃货那，吃爽最重要啦，饿不饿，找个人给你下碗面吃？','吃饭不积极，思想有问题！'],
			welTxt=(new Date()%2)?timeTxt[current]:rdTxt[parseInt((rdTxt.length-1)*Math.random())];
			
			$('wel').template('tmpl_wel',{welTxt:welTxt,welImg:'images/welcome/'+current+'.png'});
			
			setTimeout(function(){
				$('fk').transition().css('top','233px').css('left','430px');
				setTimeout(function(){
					$('fk').totate(30);
				},600)
			},2000);
			
		},
		//用户食品选择
		dish:function(){
			var params={
				'interface':'get_dish_list',
				'length':'16'
			};
			for(var p in pre){
				params[p]=pre[p];
			}
			fd.http.post('/v1/api.php',JSON.stringify(params),function(json){
				var data=json['data'],page=0,current=1,selected=[];
				
				while(page++,data.length){
					var arr=data.splice(0,2),json={};
					for(var i=0;i<arr.length;i++){
						for(var p in arr[i]){
							json[p+'_'+i]=arr[i][p];
						}
					}
					
					var div=document.createElement('div');
					div.setAttribute('page',page);
					var elem=$(div);
					elem.cls('page').css('zIndex',100+page).template('tmpl_dish',json);
					$('dish').append(div);
					
					elem.evt('click',function(evt,target){
						var dep=0;
						while(!target.getAttribute('pos')&&4>++dep){
							target=target.parentNode;
						}
						if(dep>=3){
							return;
						}
						var pos=false,dish,parent=target.parentNode,divs=parent.getElementsByTagName('div'),left,right;
						//数据记录
						(dish=target.getAttribute('dish'))&&selected.push(dish);
						for(var i=0;i<divs.length;i++){
							(pos=divs[i].getAttribute('pos'))&&(pos=='left'?left=divs[i]:right=divs[i]);
						}
						
						//页码更新
						current++;
						//判断是否是最后一页
						if(current==page){
							parent.parentNode.removeChild(parent);
							$('page').hide();
							fd.action.recommend(selected);
							return;
						}
						
						$(left).transition().css('left','-570px').css('opacity',1);
						$(right).transition().css('right','-570px').css('opacity',1);

						setTimeout(function(){
							parent.parentNode.removeChild(parent);
						},600);
						
						
						
						$('page').template('tmpl_page',{current:current,total:page-1});
						
	
					})
				}
				
				for(var i=0;i<data.length;i++){
					$('dish_'+i).template('tmpl_dish',data[i]);
				}
				
				$('page').template('tmpl_page',{current:current,total:page-1});
			});
		},
		//推荐结果信息
		recommend:function(arg){
			var params={
				'interface':'get_recommend_accounts',
				'selected':arg,
				'num':'1'
			};
			for(var p in pre){
				params[p]=pre[p];
			}
			fd.http.post('/v1/api.php',JSON.stringify(params),function(json){
				$('wrap_food').hide();
				
				var data=json['data'][0];
				
				
				
				$('commend_user').template('tmpl_commend_user',{
					face:data['open_id'],
					nick_name:data['nick_name'],
					city:data['city'],
					sex:data['gender']*1?'男':'女',
					sim:'甜食、粤菜'
				});
				
				var history=[
					
					{nick_name:'胥晓欢',city:'深圳',sex:'男',face:'b'},
					{nick_name:'萧国翘',city:'深圳',sex:'男',face:'c'},
					{nick_name:'周军',city:'深圳',sex:'男',face:'e'},
					{nick_name:'黄希彤',city:'深圳',sex:'男',face:'f'},
					{nick_name:'陈子舜',city:'深圳',sex:'男',face:'g'},
					{nick_name:'张四',city:'深圳',sex:'男',face:'d'},
					{nick_name:'张一',city:'深圳',sex:'男',face:'a'},
					{nick_name:'蒋延生',city:'深圳',sex:'男',face:'h'},
					{nick_name:'林挺',city:'深圳',sex:'男',face:'i'},
					{nick_name:'马化腾',city:'深圳',sex:'男',face:'j'}
				];
				var historyMax=Math.floor(history.length/2);
				var historyElem=$('history');
				historyElem.template('tmpl_history',history);
				
				$('wrap_recommend').show();
				
				fd.cache.pn=1;
				$('side_left').evt('click',function(evt,target,elem){
					var pn=fd.cache.pn;
					pn-1>=1&&(fd.cache.pn--,historyElem.desk(860,fd.cache.pn),$('side_right').cls('side_right_green'));
					fd.cache.pn-1<1&&$(elem).cls('side_left_gray');
				});
				$('side_right').evt('click',function(evt,target,elem){
					var pn=fd.cache.pn;
					pn+1<=historyMax&&(fd.cache.pn++,historyElem.desk(860,fd.cache.pn),$('side_left').cls('side_left_green'));
					fd.cache.pn+1>historyMax&&$(elem).cls('side_right_gray');
				});
				
				$('partner_history').transition(0.3).css('overflow','hidden');
				/*$('pn_unfold').evt('click',function(evt,target,elem){
					$("partner_history").show().css('height','200px');
					$('pn_unfold').hide();
				});*/
				
				var state=0;
				$('pn_fold').evt('click',function(evt,target,elem){
					var ph=$("partner_history"), parent=ph[0].parentNode;
					parent.className=='old_partner_div_close'?(parent.className='old_partner_div',ph.css('height','173px'))
					:(parent.className='old_partner_div_close',ph.css('height','0px'));		
				})
			});
		}
		
	};
})(this.Food);

/**
 * 执行action
 */
(function(fd){
	var scripts=document.getElementsByTagName('script'),action;
	for(var l=scripts.length,i=l-1;0<=i;i--){
		(action=scripts[i].getAttribute('action'))&&fd.action[action]();
	}
})(this.Food);
